﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;

namespace FisheryPlatform.Bussiness.WebRuntime
{
    public class HttpHandlerBase
    {
        /// <summary>
        /// 用本类别的属性必须调用此方法
        /// </summary>
        /// <param name="context"></param>
        public void Init(HttpContext context)
        {
            Request = context.Request;
            Response = context.Response;
        }

        public HttpRequest Request
        {
            get;
            set;
        }

        public HttpResponse Response
        {
            get;
            set;
        }

        public string ContentType
        {
            get
            {
                return this.Response.ContentType;
            }
            set
            {
                Response.ContentType = value;
            }
        }

        public string FormValue(string name)
        {
            return Request.Form[name];
        }

        /// <summary>
        /// 指定类序列化成json 并放到输出流中
        /// </summary>
        /// <param name="obj"></param>
        public void SerializeDataToResponseStream(object obj)
        {
            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
            StringBuilder jsonText = new StringBuilder();
            jsSerializer.Serialize(obj, jsonText);
            Response.Write(jsonText);
        }

        /// <summary>
        /// 序列化数据
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string SerializeData(object obj)
        {
            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
            StringBuilder jsonText = new StringBuilder();
            jsSerializer.Serialize(obj, jsonText);
            return jsonText.ToString();
        }
    }
}
